/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.UtilidadesDAO;
import org.trandio.dao.UtilidadesInterface;
import org.trandio.hb.TbConfigAmbiente;
import org.trandio.hb.TbConfigUsuario;

/**
 *
 * @author root
 */
public class UsuarioServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            UtilidadesInterface iface = new UtilidadesDAO();

            int idUsuario = Integer.parseInt(request.getParameter("idUsuario").trim());

            switch (Integer.parseInt(request.getParameter("op"))) {
                case 1:

                    //tipo 1: si chequeado, tipo 2: no chequeado

                    //configurar ambiente                    
                    TbConfigAmbiente ambiente = new TbConfigAmbiente();
                    ambiente.setIdUsuario(Integer.parseInt(request.getParameter("idUsuario").trim()));
                    ambiente.setIdConfigUsuario(Integer.parseInt(request.getParameter("idConfigUsuario").trim()));
                    ambiente.setId(Integer.parseInt(request.getParameter("idAmbiente").trim()));

                    if (request.getParameter("tipo").equals("1")) {
                        ambiente.setActivo("T");
                        iface.actualizarConfigAmbiente(Integer.parseInt(request.getParameter("idAmbiente").trim()), "T");
                    } else {
                        iface.actualizarConfigAmbiente(Integer.parseInt(request.getParameter("idAmbiente").trim()), "F");
                    }

                    if (Integer.parseInt(request.getParameter("idAmbiente").trim()) == 0) {
                        iface.guardarConfigAmbiente(ambiente);
                    }

                    break;

                case 2:
                    //traer configuracion
                    List<TbConfigUsuario> config = new ArrayList<TbConfigUsuario>();
                    config = iface.consultarConfigUsuario();
                    TbConfigAmbiente amb = new TbConfigAmbiente();


                    Iterator it = config.iterator();
                    out.println("<table align='center' width='80%'>");
                    while (it.hasNext()) {
                        TbConfigUsuario object = (TbConfigUsuario) it.next();

                        amb = iface.consultarConfigAmbiente(object.getId(), idUsuario);
                        int idAmb = 0;
                        String attr = "";

                        if (amb != null) {
                            if (amb.getActivo().equals("T")) {
                                attr = "checked";
                            } else {
                                attr = "";
                            }

                            idAmb = amb.getId();
                        }




                        out.println("<tr>"
                                + "<td>" + object.getDescripcion() + "</td>"
                                + "<td><input type='checkbox' " + attr + " id='" + object.getCodigo() + "' onclick='guardarConfigAmbiente(this," + idUsuario + "," + object.getId() + "," + idAmb + ")'/></td>"
                                + "</tr>");
                    }
                    out.println("</table>");

                    break;

            }


        } finally {
            out.close();
        }
    }

   // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
